package com.zjuee.web.warning.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.Pages;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.dto.common.warning.AlarmPromptDto;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.base.enums.RedisCacheType;
import com.zjuee.base.warning.AlarmPrompt;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.translation.utils.CaseUtil;
import com.zjuee.web.warning.mapper.AlarmPromptMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import com.zjuee.service.AlarmPromptService;
import com.zjuee.vo.AlarmPromptVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@ComponentScan(value={"com.zjuee.common.translation.utils","com.zjuee.common.translation.service"})
@Slf4j
public class AlarmPromptServiceImpl implements AlarmPromptService {
    @Resource
    AlarmPromptMapper alarmPromptMapper;

    @Resource
    LoginDto loginDto;
    @Resource
    TranslateData translateData;
    @Override
    public Page<AlarmPromptVo> listInfo(AlarmPromptDto alarmPromptDto) {
        if (!Optional.ofNullable(alarmPromptDto.getPage()).isPresent()) {
            alarmPromptDto.setPage(new Pages());
        }
        alarmPromptDto.setBciId(loginDto.getOrgBusinessId());
        Page<AlarmPromptVo> result=alarmPromptMapper.listInfo(alarmPromptDto.getPage().convert(),alarmPromptDto);
        List<AlarmPromptVo> list=(List<AlarmPromptVo>) translate(result.getRecords(),"list");
        Page<AlarmPromptVo> page=new Page<>();
        BeanUtils.copyProperties(result,page);
        page.setRecords(list);
        return page;
    }

    @Override
    public AlarmPromptVo selectByIdForDetail(Long id, Long validateId) {
        ArrayList<Object> list= new ArrayList<Object>();
        AlarmPromptVo alarm = alarmPromptMapper.selectByIdForDetail(id);
        list.add(alarm);
        List<AlarmPromptVo> lists=(List<AlarmPromptVo>) translate(list,"list");
        return lists.get(0);
    }

    @Override
    public Boolean update(AlarmPromptDto alarmPromptDto) {
        AlarmPrompt alarmPrompt =new AlarmPrompt();
        BeanUtils.copyProperties(alarmPromptDto,alarmPrompt);
        alarmPromptDto.setWapStatus("1");
        alarmPromptDto.setWapModifier(loginDto.getUserName());
        alarmPromptDto.setWapModifiedTime(new Date());
        return alarmPromptMapper.updateById(alarmPromptDto)>0;
    }

    private Object translate(Object list, String returnType){
        Object obj=translateData.translateData(
                list,"alarmPromptHandle",
                String.valueOf(DictionaryType.BAR_LEVEL.getValue()),
                String.valueOf(DictionaryType.WAP_STATUS.getValue()),
                String.valueOf(DictionaryType.WAP_NAME.getValue()),
                String.valueOf(DictionaryType.BAR_TYPE.getValue()),
                String.valueOf(RedisCacheType.PROVINCE),
                String.valueOf(RedisCacheType.CITY),
                String.valueOf(RedisCacheType.AREA),
                String.valueOf(RedisCacheType.STREET)
        );
        if(StringUtils.equalsIgnoreCase(returnType, "map")){
            return obj;
        }
        return CaseUtil
                .castList(obj, AlarmPromptVo.class);
    }
}
